<?php
/*
 * Copyright (c) 2011 Todd Brady
 *
 * PS3Theatre is freely distributable under the terms of the MIT license.
 * For details, see the MIT-Liscence web site: http://www.opensource.org/licenses/mit-license.php 
 */
 	include("includes/db_connect.php");
	
	$typeString = "TV Shows";
	$id = $_GET['id'];
	$action = $_GET['action'];
	$name = "";
	$url = "";
	
	//if action = edit we need to fill out form
	if($action == 'Edit'){
		$themeQuery = "SELECT id, name, url from tbl_themes where id=".$id;
		$theme=mysql_query($themeQuery);
		$themeRow = mysql_fetch_array($theme);
		$name = $themeRow['name'];
		$url = $themeRow['url'];
	
	//if action is delete, just delete and redirect
	}else if($action == 'Del' && $id != ''){
		$deleteQuery = "DELETE from tbl_themes WHERE id=$id";
		$delete=mysql_query($deleteQuery);
		$message = "TV Show theme sucessfully deleted.";
		
		//redirect to tv show setup with message
		header("Location: tvshows-setup.php?message=".$message);
	}

	if(isset($_POST['id']))
	{
		$id = $_POST['id'];
		$name = $_POST['name'];
		$url = $_POST['url'];
		$message = "";
		
		//insert
		if($id == ''){
			$insertQuery = sprintf("insert into tbl_themes (type_id,name,url) select id, '%s', '%s' from tbl_types where type='%s'",
							mysql_real_escape_string($name),
							mysql_real_escape_string($url),
							mysql_real_escape_string($typeString));
			$insert=mysql_query($insertQuery) or die('error inserting: '.mysql_error());
			$message = "TV Show theme successfully created.";
		}
		//update
		else{
			$updateQuery = sprintf("update tbl_themes set name='%s', url='%s' where id=%s",
							mysql_real_escape_string($name),
							mysql_real_escape_string($url),
							mysql_real_escape_string($id));
			$update=mysql_query($updateQuery);
			$message = "TV Show theme successfully updated.";
		}
	
		//redirect to tv show setup with message
		header("Location: tvshows-setup.php?message=".$message);
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<LINK href="../css/main.css" rel="stylesheet" type="text/css">
</head>
<body>
<h4><?php echo $action;?> TV Show Theme </h4>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="id" value="<?php echo $id;?>" />
<table cellpadding="2" class="rowmajor">
 <tbody><tr>
 <th class="vt"><div align="left">Theme Name :</div></th>
 <td><input name="name" type="text" id="name" value="<?php echo $name;?>" size="50" maxlength="512"/></td>
 </tr>
   <tr>
     <th class="vt"><div align="left">Url:</div></th>
     <td><input name="url" type="text" id="url" value="<?php echo $url;?>" size="50" maxlength="512"/></td>
     </tr>
 </tbody></table>
<p>
  <label>
  <input type="submit" name="Submit" value="Save" />
  </label>
  <input type="button" name="cancelButton" value="Cancel" onclick="location='tvshows-setup.php'"/>
</p>
</form>
</body>
</html>
